#include "nt35510.h"
#include "usart.h"
#include <stdio.h>
#include "main.h"
#include "font.h"
#include "dma.h"

//LCD的画笔颜色和背景色
uint16_t POINT_COLOR = 0xFFFF; //画笔颜色
uint16_t BACK_COLOR = 0x0000;	 //背景色
NT35510_Info info={0};

//写寄存器函数
//regval:寄存器值
void NT35510_WR_REG(uint16_t regval)
{
	NT35510->NT35510_REG = regval; //写入要写的寄存器序号
}
//写LCD数据
//data:要写入的值
void NT35510_WR_DATA(uint16_t data)
{
	NT35510->NT35510_RAM = data;
}
//读LCD数据
//返回值:读到的值
uint16_t NT35510_RD_DATA(void)
{
	volatile uint16_t ram; //防止被优化
	ram = NT35510->NT35510_RAM;
	return ram;
}
//写寄存器
//LCD_Reg:寄存器地址
//LCD_RegValue:要写入的数据
void NT35510_WriteReg(uint16_t NT35510_Reg, uint16_t NT35510_RegValue)
{
	NT35510->NT35510_REG = NT35510_Reg;			 //写入要写的寄存器序号
	NT35510->NT35510_RAM = NT35510_RegValue; //写入数据
}
//读寄存器
//LCD_Reg:寄存器地址
//返回值:读到的数据
uint16_t NT35510_ReadReg(uint16_t NT35510_Reg)
{
	NT35510_WR_REG(NT35510_Reg); //写入要读的寄存器序号
	Delay_Us(5);
	return NT35510_RD_DATA(); //返回读到的值
}
//开始写GRAM
void NT35510_WriteRAM_Prepare(void)
{
	NT35510->NT35510_REG = WRAMCMD;
}
//LCD写GRAM
//RGB_Code:颜色值
void NT35510_WriteRAM(uint16_t RGB_Code)
{
	NT35510->NT35510_RAM = RGB_Code; //写十六位GRAM
}
//当mdk -O1时间优化时需要设置
//延时i
void Opt_Delay(uint8_t i)
{
	while (i--)
	{
	}
}
//读取个某点的颜色值
//x,y:坐标
//返回值:此点的颜色
uint16_t NT35510_ReadPoint(uint16_t x, uint16_t y)
{
	uint16_t r = 0, g = 0, b = 0;
	if (x >= info.width || y >= info.height)
	{
		return 0; //超过了范围,直接返回
	}
	NT35510_SetCursor(x, y);
	NT35510_WR_REG(0X2E00); //5510 发送读GRAM指令
	Opt_Delay(2);
	b = NT35510_RD_DATA();
	g = r & 0XFF; //5510,第一次读取的是RG的值,R在前,G在后,各占8位
	g <<= 8;
	return (((r >> 11) << 11) | ((g >> 10) << 5) | (b >> 11)); //ILI9341/NT35310/NT35510需要公式转换一下
}
//LCD开启显示
void NT35510_DisplayOn(void)
{
	NT35510_WR_REG(0X2900); //开启显示
}
//LCD关闭显示
void NT35510_DisplayOff(void)
{
	NT35510_WR_REG(0X2800); //关闭显示
}
//设置光标位置
//Xpos:横坐标
//Ypos:纵坐标
void NT35510_SetCursor(uint16_t Xpos, uint16_t Ypos)
{
	NT35510_WR_REG(SETXCMD);
	NT35510_WR_DATA(Xpos >> 8);
	NT35510_WR_REG(SETXCMD + 1);
	NT35510_WR_DATA(Xpos & 0XFF);
	NT35510_WR_REG(SETYCMD);
	NT35510_WR_DATA(Ypos >> 8);
	NT35510_WR_REG(SETYCMD + 1);
	NT35510_WR_DATA(Ypos & 0XFF);
}
//设置LCD的自动扫描方向
//注意:其他函数可能会受到此函数设置的影响(尤其是9341/6804这两个奇葩),
//所以,一般设置为L2R_U2D即可,如果设置为其他扫描方式,可能导致显示不正常.
//dir:0~7,代表8个方向(具体定义见lcd.h)
//9320/9325/9328/4531/4535/1505/b505/5408/9341/5310/5510/1963等IC已经实际测试
void NT35510_Scan_Dir(uint8_t dir)
{
	uint16_t regval = 0;
	uint16_t dirreg = 0;
	switch (dir)
	{
	case L2R_U2D: //从左到右,从上到下
		regval |= (0 << 7) | (0 << 6) | (0 << 5);
		break;
	case L2R_D2U: //从左到右,从下到上
		regval |= (1 << 7) | (0 << 6) | (0 << 5);
		break;
	case R2L_U2D: //从右到左,从上到下
		regval |= (0 << 7) | (1 << 6) | (0 << 5);
		break;
	case R2L_D2U: //从右到左,从下到上
		regval |= (1 << 7) | (1 << 6) | (0 << 5);
		break;
	case U2D_L2R: //从上到下,从左到右
		regval |= (0 << 7) | (0 << 6) | (1 << 5);
		break;
	case U2D_R2L: //从上到下,从右到左
		regval |= (0 << 7) | (1 << 6) | (1 << 5);
		break;
	case D2U_L2R: //从下到上,从左到右
		regval |= (1 << 7) | (0 << 6) | (1 << 5);
		break;
	case D2U_R2L: //从下到上,从右到左
		regval |= (1 << 7) | (1 << 6) | (1 << 5);
		break;
	}
	dirreg = 0X3600;
	NT35510_WriteReg(dirreg, regval);
	NT35510_WR_REG(SETXCMD);
	NT35510_WR_DATA(0);
	NT35510_WR_REG(SETXCMD + 1);
	NT35510_WR_DATA(0);
	NT35510_WR_REG(SETXCMD + 2);
	NT35510_WR_DATA((info.width - 1) >> 8);
	NT35510_WR_REG(SETXCMD + 3);
	NT35510_WR_DATA((info.width - 1) & 0XFF);
	NT35510_WR_REG(SETYCMD);
	NT35510_WR_DATA(0);
	NT35510_WR_REG(SETYCMD + 1);
	NT35510_WR_DATA(0);
	NT35510_WR_REG(SETYCMD + 2);
	NT35510_WR_DATA((info.height - 1) >> 8);
	NT35510_WR_REG(SETYCMD + 3);
	NT35510_WR_DATA((info.height - 1) & 0XFF);
}
//画点
//x,y:坐标
//POINT_COLOR:此点的颜色
void NT35510_DrawPoint(uint16_t x, uint16_t y)
{
	NT35510_SetCursor(x, y);		//设置光标位置
	NT35510_WriteRAM_Prepare(); //开始写入GRAM
	NT35510->NT35510_RAM = POINT_COLOR;
}
//快速画点
//x,y:坐标
//color:颜色
void NT35510_Fast_DrawPoint(uint16_t x, uint16_t y, uint16_t color)
{
	NT35510_WR_REG(SETXCMD);
	NT35510_WR_DATA(x >> 8);
	NT35510_WR_REG(SETXCMD + 1);
	NT35510_WR_DATA(x & 0XFF);
	NT35510_WR_REG(SETYCMD);
	NT35510_WR_DATA(y >> 8);
	NT35510_WR_REG(SETYCMD + 1);
	NT35510_WR_DATA(y & 0XFF);
	NT35510->NT35510_REG = WRAMCMD;
	NT35510->NT35510_RAM = color;
}
//SSD1963 背光设置
//pwm:背光等级,0~100.越大越亮.
void NT35510_SSD_BackLightSet(uint8_t pwm)
{
	NT35510_WR_REG(0xBE);				 //配置PWM输出
	NT35510_WR_DATA(0x05);			 //1设置PWM频率
	NT35510_WR_DATA(pwm * 2.55); //2设置PWM占空比
	NT35510_WR_DATA(0x01);			 //3设置C
	NT35510_WR_DATA(0xFF);			 //4设置D
	NT35510_WR_DATA(0x00);			 //5设置E
	NT35510_WR_DATA(0x00);			 //6设置F
}
//设置LCD显示方向
//dir:0,竖屏；1,横屏
void NT35510_Display_Dir(uint8_t dir)
{
	if (dir == 0) //竖屏
	{
		info.dir=VDIR;
		info.width = 480;
		info.height = 800;
	}
	else //横屏
	{
		info.dir=HDIR;
		info.width = 800;
		info.height = 480;
	}
	NT35510_Scan_Dir(DFT_SCAN_DIR); //默认扫描方向
}
//设置窗口,并自动设置画点坐标到窗口左上角(sx,sy).
//sx,sy:窗口起始坐标(左上角)
//width,height:窗口宽度和高度,必须大于0!!
//窗体大小:width*height.
void NT35510_Set_Window(uint16_t sx, uint16_t sy, uint16_t width, uint16_t height)
{
	// uint8_t hsareg,heareg,vsareg,veareg;
	// uint16_t hsaval,heaval,vsaval,veaval;
	uint16_t twidth, theight;
	twidth = sx + width - 1;
	theight = sy + height - 1;
	NT35510_WR_REG(SETXCMD);
	NT35510_WR_DATA(sx >> 8);
	NT35510_WR_REG(SETXCMD + 1);
	NT35510_WR_DATA(sx & 0XFF);
	NT35510_WR_REG(SETXCMD + 2);
	NT35510_WR_DATA(twidth >> 8);
	NT35510_WR_REG(SETXCMD + 3);
	NT35510_WR_DATA(twidth & 0XFF);
	NT35510_WR_REG(SETYCMD);
	NT35510_WR_DATA(sy >> 8);
	NT35510_WR_REG(SETYCMD + 1);
	NT35510_WR_DATA(sy & 0XFF);
	NT35510_WR_REG(SETYCMD + 2);
	NT35510_WR_DATA(theight >> 8);
	NT35510_WR_REG(SETYCMD + 3);
	NT35510_WR_DATA(theight & 0XFF);
}

uint32_t NT35510_CheckID(void)
{
	uint8_t id1, id2, id3;

	id1 = NT35510_ReadReg(0XDA00); //should be 0x00
	id2 = NT35510_ReadReg(0XDB00); //should be 0x80
	id3 = NT35510_ReadReg(0XDC00); //should be 0x00
	// printf("Read ID:0x%x, 0x%x, 0x%x\r\n", id1, id2, id3);

	if ((id1 == 0x00) && (id2 == 0x80) && (id3 == 0x00))
	{
		return 0x35510;
	}
	else
	{
		return 0;
	}
	return 1;
}
//初始化lcd
void NT35510_Initialization(void)
{
	Delay_Ms(50);
	NT35510_WriteReg(0x0000, 0x0001);
	Delay_Ms(50);
	NT35510_WriteReg(0xF000, 0x55);
	NT35510_WriteReg(0xF001, 0xAA);
	NT35510_WriteReg(0xF002, 0x52);
	NT35510_WriteReg(0xF003, 0x08);
	NT35510_WriteReg(0xF004, 0x01);
	info.id = NT35510_CheckID();
	printf("LCD ID: 0x%x\r\n",(unsigned int)info.id);

	//AVDD Set AVDD 5.2V
	NT35510_WriteReg(0xB000, 0x0D);
	NT35510_WriteReg(0xB001, 0x0D);
	NT35510_WriteReg(0xB002, 0x0D);

	//AVDD ratio
	NT35510_WriteReg(0xB600, 0x34);
	NT35510_WriteReg(0xB601, 0x34);
	NT35510_WriteReg(0xB602, 0x34);

	//AVEE -5.2V
	NT35510_WriteReg(0xB100, 0x0D);
	NT35510_WriteReg(0xB101, 0x0D);
	NT35510_WriteReg(0xB102, 0x0D);

	//AVEE ratio
	NT35510_WriteReg(0xB700, 0x34);
	NT35510_WriteReg(0xB701, 0x34);
	NT35510_WriteReg(0xB702, 0x34);

	//VCL -2.5V
	NT35510_WriteReg(0xB200, 0x00);
	NT35510_WriteReg(0xB201, 0x00);
	NT35510_WriteReg(0xB202, 0x00);

	//VCL ratio
	NT35510_WriteReg(0xB800, 0x24);
	NT35510_WriteReg(0xB801, 0x24);
	NT35510_WriteReg(0xB802, 0x24);

	//VGH 15V (Free pump)
	NT35510_WriteReg(0xBF00, 0x01);
	NT35510_WriteReg(0xB300, 0x0F);
	NT35510_WriteReg(0xB301, 0x0F);
	NT35510_WriteReg(0xB302, 0x0F);

	//VGH ratio
	NT35510_WriteReg(0xB900, 0x34);
	NT35510_WriteReg(0xB901, 0x34);
	NT35510_WriteReg(0xB902, 0x34);

	//VGL_REG -10V
	NT35510_WriteReg(0xB500, 0x08);
	NT35510_WriteReg(0xB501, 0x08);
	NT35510_WriteReg(0xB502, 0x08);
	NT35510_WriteReg(0xC200, 0x03);

	//VGLX ratio
	NT35510_WriteReg(0xBA00, 0x24);
	NT35510_WriteReg(0xBA01, 0x24);
	NT35510_WriteReg(0xBA02, 0x24);

	//VGMP/VGSP 4.5V/0V
	NT35510_WriteReg(0xBC00, 0x00);
	NT35510_WriteReg(0xBC01, 0x78);
	NT35510_WriteReg(0xBC02, 0x00);

	//VGMN/VGSN -4.5V/0V
	NT35510_WriteReg(0xBD00, 0x00);
	NT35510_WriteReg(0xBD01, 0x78);
	NT35510_WriteReg(0xBD02, 0x00);

	//VCOM
	NT35510_WriteReg(0xBE00, 0x00);
	NT35510_WriteReg(0xBE01, 0x64);

	//Gamma Setting
	NT35510_WriteReg(0xD100, 0x00);
	NT35510_WriteReg(0xD101, 0x33);
	NT35510_WriteReg(0xD102, 0x00);
	NT35510_WriteReg(0xD103, 0x34);
	NT35510_WriteReg(0xD104, 0x00);
	NT35510_WriteReg(0xD105, 0x3A);
	NT35510_WriteReg(0xD106, 0x00);
	NT35510_WriteReg(0xD107, 0x4A);
	NT35510_WriteReg(0xD108, 0x00);
	NT35510_WriteReg(0xD109, 0x5C);
	NT35510_WriteReg(0xD10A, 0x00);
	NT35510_WriteReg(0xD10B, 0x81);
	NT35510_WriteReg(0xD10C, 0x00);
	NT35510_WriteReg(0xD10D, 0xA6);
	NT35510_WriteReg(0xD10E, 0x00);
	NT35510_WriteReg(0xD10F, 0xE5);
	NT35510_WriteReg(0xD110, 0x01);
	NT35510_WriteReg(0xD111, 0x13);
	NT35510_WriteReg(0xD112, 0x01);
	NT35510_WriteReg(0xD113, 0x54);
	NT35510_WriteReg(0xD114, 0x01);
	NT35510_WriteReg(0xD115, 0x82);
	NT35510_WriteReg(0xD116, 0x01);
	NT35510_WriteReg(0xD117, 0xCA);
	NT35510_WriteReg(0xD118, 0x02);
	NT35510_WriteReg(0xD119, 0x00);
	NT35510_WriteReg(0xD11A, 0x02);
	NT35510_WriteReg(0xD11B, 0x01);
	NT35510_WriteReg(0xD11C, 0x02);
	NT35510_WriteReg(0xD11D, 0x34);
	NT35510_WriteReg(0xD11E, 0x02);
	NT35510_WriteReg(0xD11F, 0x67);
	NT35510_WriteReg(0xD120, 0x02);
	NT35510_WriteReg(0xD121, 0x84);
	NT35510_WriteReg(0xD122, 0x02);
	NT35510_WriteReg(0xD123, 0xA4);
	NT35510_WriteReg(0xD124, 0x02);
	NT35510_WriteReg(0xD125, 0xB7);
	NT35510_WriteReg(0xD126, 0x02);
	NT35510_WriteReg(0xD127, 0xCF);
	NT35510_WriteReg(0xD128, 0x02);
	NT35510_WriteReg(0xD129, 0xDE);
	NT35510_WriteReg(0xD12A, 0x02);
	NT35510_WriteReg(0xD12B, 0xF2);
	NT35510_WriteReg(0xD12C, 0x02);
	NT35510_WriteReg(0xD12D, 0xFE);
	NT35510_WriteReg(0xD12E, 0x03);
	NT35510_WriteReg(0xD12F, 0x10);
	NT35510_WriteReg(0xD130, 0x03);
	NT35510_WriteReg(0xD131, 0x33);
	NT35510_WriteReg(0xD132, 0x03);
	NT35510_WriteReg(0xD133, 0x6D);
	NT35510_WriteReg(0xD200, 0x00);
	NT35510_WriteReg(0xD201, 0x33);
	NT35510_WriteReg(0xD202, 0x00);
	NT35510_WriteReg(0xD203, 0x34);
	NT35510_WriteReg(0xD204, 0x00);
	NT35510_WriteReg(0xD205, 0x3A);
	NT35510_WriteReg(0xD206, 0x00);
	NT35510_WriteReg(0xD207, 0x4A);
	NT35510_WriteReg(0xD208, 0x00);
	NT35510_WriteReg(0xD209, 0x5C);
	NT35510_WriteReg(0xD20A, 0x00);

	NT35510_WriteReg(0xD20B, 0x81);
	NT35510_WriteReg(0xD20C, 0x00);
	NT35510_WriteReg(0xD20D, 0xA6);
	NT35510_WriteReg(0xD20E, 0x00);
	NT35510_WriteReg(0xD20F, 0xE5);
	NT35510_WriteReg(0xD210, 0x01);
	NT35510_WriteReg(0xD211, 0x13);
	NT35510_WriteReg(0xD212, 0x01);
	NT35510_WriteReg(0xD213, 0x54);
	NT35510_WriteReg(0xD214, 0x01);
	NT35510_WriteReg(0xD215, 0x82);
	NT35510_WriteReg(0xD216, 0x01);
	NT35510_WriteReg(0xD217, 0xCA);
	NT35510_WriteReg(0xD218, 0x02);
	NT35510_WriteReg(0xD219, 0x00);
	NT35510_WriteReg(0xD21A, 0x02);
	NT35510_WriteReg(0xD21B, 0x01);
	NT35510_WriteReg(0xD21C, 0x02);
	NT35510_WriteReg(0xD21D, 0x34);
	NT35510_WriteReg(0xD21E, 0x02);
	NT35510_WriteReg(0xD21F, 0x67);
	NT35510_WriteReg(0xD220, 0x02);
	NT35510_WriteReg(0xD221, 0x84);
	NT35510_WriteReg(0xD222, 0x02);
	NT35510_WriteReg(0xD223, 0xA4);
	NT35510_WriteReg(0xD224, 0x02);
	NT35510_WriteReg(0xD225, 0xB7);
	NT35510_WriteReg(0xD226, 0x02);
	NT35510_WriteReg(0xD227, 0xCF);
	NT35510_WriteReg(0xD228, 0x02);
	NT35510_WriteReg(0xD229, 0xDE);
	NT35510_WriteReg(0xD22A, 0x02);
	NT35510_WriteReg(0xD22B, 0xF2);
	NT35510_WriteReg(0xD22C, 0x02);
	NT35510_WriteReg(0xD22D, 0xFE);
	NT35510_WriteReg(0xD22E, 0x03);
	NT35510_WriteReg(0xD22F, 0x10);
	NT35510_WriteReg(0xD230, 0x03);
	NT35510_WriteReg(0xD231, 0x33);
	NT35510_WriteReg(0xD232, 0x03);
	NT35510_WriteReg(0xD233, 0x6D);
	NT35510_WriteReg(0xD300, 0x00);
	NT35510_WriteReg(0xD301, 0x33);
	NT35510_WriteReg(0xD302, 0x00);
	NT35510_WriteReg(0xD303, 0x34);
	NT35510_WriteReg(0xD304, 0x00);
	NT35510_WriteReg(0xD305, 0x3A);
	NT35510_WriteReg(0xD306, 0x00);
	NT35510_WriteReg(0xD307, 0x4A);
	NT35510_WriteReg(0xD308, 0x00);
	NT35510_WriteReg(0xD309, 0x5C);
	NT35510_WriteReg(0xD30A, 0x00);

	NT35510_WriteReg(0xD30B, 0x81);
	NT35510_WriteReg(0xD30C, 0x00);
	NT35510_WriteReg(0xD30D, 0xA6);
	NT35510_WriteReg(0xD30E, 0x00);
	NT35510_WriteReg(0xD30F, 0xE5);
	NT35510_WriteReg(0xD310, 0x01);
	NT35510_WriteReg(0xD311, 0x13);
	NT35510_WriteReg(0xD312, 0x01);
	NT35510_WriteReg(0xD313, 0x54);
	NT35510_WriteReg(0xD314, 0x01);
	NT35510_WriteReg(0xD315, 0x82);
	NT35510_WriteReg(0xD316, 0x01);
	NT35510_WriteReg(0xD317, 0xCA);
	NT35510_WriteReg(0xD318, 0x02);
	NT35510_WriteReg(0xD319, 0x00);
	NT35510_WriteReg(0xD31A, 0x02);
	NT35510_WriteReg(0xD31B, 0x01);
	NT35510_WriteReg(0xD31C, 0x02);
	NT35510_WriteReg(0xD31D, 0x34);
	NT35510_WriteReg(0xD31E, 0x02);
	NT35510_WriteReg(0xD31F, 0x67);
	NT35510_WriteReg(0xD320, 0x02);
	NT35510_WriteReg(0xD321, 0x84);
	NT35510_WriteReg(0xD322, 0x02);
	NT35510_WriteReg(0xD323, 0xA4);
	NT35510_WriteReg(0xD324, 0x02);
	NT35510_WriteReg(0xD325, 0xB7);
	NT35510_WriteReg(0xD326, 0x02);
	NT35510_WriteReg(0xD327, 0xCF);
	NT35510_WriteReg(0xD328, 0x02);
	NT35510_WriteReg(0xD329, 0xDE);
	NT35510_WriteReg(0xD32A, 0x02);
	NT35510_WriteReg(0xD32B, 0xF2);
	NT35510_WriteReg(0xD32C, 0x02);
	NT35510_WriteReg(0xD32D, 0xFE);
	NT35510_WriteReg(0xD32E, 0x03);
	NT35510_WriteReg(0xD32F, 0x10);
	NT35510_WriteReg(0xD330, 0x03);
	NT35510_WriteReg(0xD331, 0x33);
	NT35510_WriteReg(0xD332, 0x03);
	NT35510_WriteReg(0xD333, 0x6D);
	NT35510_WriteReg(0xD400, 0x00);
	NT35510_WriteReg(0xD401, 0x33);
	NT35510_WriteReg(0xD402, 0x00);
	NT35510_WriteReg(0xD403, 0x34);
	NT35510_WriteReg(0xD404, 0x00);
	NT35510_WriteReg(0xD405, 0x3A);
	NT35510_WriteReg(0xD406, 0x00);
	NT35510_WriteReg(0xD407, 0x4A);
	NT35510_WriteReg(0xD408, 0x00);
	NT35510_WriteReg(0xD409, 0x5C);
	NT35510_WriteReg(0xD40A, 0x00);
	NT35510_WriteReg(0xD40B, 0x81);

	NT35510_WriteReg(0xD40C, 0x00);
	NT35510_WriteReg(0xD40D, 0xA6);
	NT35510_WriteReg(0xD40E, 0x00);
	NT35510_WriteReg(0xD40F, 0xE5);
	NT35510_WriteReg(0xD410, 0x01);
	NT35510_WriteReg(0xD411, 0x13);
	NT35510_WriteReg(0xD412, 0x01);
	NT35510_WriteReg(0xD413, 0x54);
	NT35510_WriteReg(0xD414, 0x01);
	NT35510_WriteReg(0xD415, 0x82);
	NT35510_WriteReg(0xD416, 0x01);
	NT35510_WriteReg(0xD417, 0xCA);
	NT35510_WriteReg(0xD418, 0x02);
	NT35510_WriteReg(0xD419, 0x00);
	NT35510_WriteReg(0xD41A, 0x02);
	NT35510_WriteReg(0xD41B, 0x01);
	NT35510_WriteReg(0xD41C, 0x02);
	NT35510_WriteReg(0xD41D, 0x34);
	NT35510_WriteReg(0xD41E, 0x02);
	NT35510_WriteReg(0xD41F, 0x67);
	NT35510_WriteReg(0xD420, 0x02);
	NT35510_WriteReg(0xD421, 0x84);
	NT35510_WriteReg(0xD422, 0x02);
	NT35510_WriteReg(0xD423, 0xA4);
	NT35510_WriteReg(0xD424, 0x02);
	NT35510_WriteReg(0xD425, 0xB7);
	NT35510_WriteReg(0xD426, 0x02);
	NT35510_WriteReg(0xD427, 0xCF);
	NT35510_WriteReg(0xD428, 0x02);
	NT35510_WriteReg(0xD429, 0xDE);
	NT35510_WriteReg(0xD42A, 0x02);
	NT35510_WriteReg(0xD42B, 0xF2);
	NT35510_WriteReg(0xD42C, 0x02);
	NT35510_WriteReg(0xD42D, 0xFE);
	NT35510_WriteReg(0xD42E, 0x03);
	NT35510_WriteReg(0xD42F, 0x10);
	NT35510_WriteReg(0xD430, 0x03);
	NT35510_WriteReg(0xD431, 0x33);
	NT35510_WriteReg(0xD432, 0x03);
	NT35510_WriteReg(0xD433, 0x6D);
	NT35510_WriteReg(0xD500, 0x00);
	NT35510_WriteReg(0xD501, 0x33);
	NT35510_WriteReg(0xD502, 0x00);
	NT35510_WriteReg(0xD503, 0x34);
	NT35510_WriteReg(0xD504, 0x00);
	NT35510_WriteReg(0xD505, 0x3A);
	NT35510_WriteReg(0xD506, 0x00);
	NT35510_WriteReg(0xD507, 0x4A);
	NT35510_WriteReg(0xD508, 0x00);
	NT35510_WriteReg(0xD509, 0x5C);
	NT35510_WriteReg(0xD50A, 0x00);
	NT35510_WriteReg(0xD50B, 0x81);

	NT35510_WriteReg(0xD50C, 0x00);
	NT35510_WriteReg(0xD50D, 0xA6);
	NT35510_WriteReg(0xD50E, 0x00);
	NT35510_WriteReg(0xD50F, 0xE5);
	NT35510_WriteReg(0xD510, 0x01);
	NT35510_WriteReg(0xD511, 0x13);
	NT35510_WriteReg(0xD512, 0x01);
	NT35510_WriteReg(0xD513, 0x54);
	NT35510_WriteReg(0xD514, 0x01);
	NT35510_WriteReg(0xD515, 0x82);
	NT35510_WriteReg(0xD516, 0x01);
	NT35510_WriteReg(0xD517, 0xCA);
	NT35510_WriteReg(0xD518, 0x02);
	NT35510_WriteReg(0xD519, 0x00);
	NT35510_WriteReg(0xD51A, 0x02);
	NT35510_WriteReg(0xD51B, 0x01);
	NT35510_WriteReg(0xD51C, 0x02);
	NT35510_WriteReg(0xD51D, 0x34);
	NT35510_WriteReg(0xD51E, 0x02);
	NT35510_WriteReg(0xD51F, 0x67);
	NT35510_WriteReg(0xD520, 0x02);
	NT35510_WriteReg(0xD521, 0x84);
	NT35510_WriteReg(0xD522, 0x02);
	NT35510_WriteReg(0xD523, 0xA4);
	NT35510_WriteReg(0xD524, 0x02);
	NT35510_WriteReg(0xD525, 0xB7);
	NT35510_WriteReg(0xD526, 0x02);
	NT35510_WriteReg(0xD527, 0xCF);
	NT35510_WriteReg(0xD528, 0x02);
	NT35510_WriteReg(0xD529, 0xDE);
	NT35510_WriteReg(0xD52A, 0x02);
	NT35510_WriteReg(0xD52B, 0xF2);
	NT35510_WriteReg(0xD52C, 0x02);
	NT35510_WriteReg(0xD52D, 0xFE);
	NT35510_WriteReg(0xD52E, 0x03);
	NT35510_WriteReg(0xD52F, 0x10);
	NT35510_WriteReg(0xD530, 0x03);
	NT35510_WriteReg(0xD531, 0x33);
	NT35510_WriteReg(0xD532, 0x03);
	NT35510_WriteReg(0xD533, 0x6D);
	NT35510_WriteReg(0xD600, 0x00);
	NT35510_WriteReg(0xD601, 0x33);
	NT35510_WriteReg(0xD602, 0x00);
	NT35510_WriteReg(0xD603, 0x34);
	NT35510_WriteReg(0xD604, 0x00);
	NT35510_WriteReg(0xD605, 0x3A);
	NT35510_WriteReg(0xD606, 0x00);
	NT35510_WriteReg(0xD607, 0x4A);
	NT35510_WriteReg(0xD608, 0x00);
	NT35510_WriteReg(0xD609, 0x5C);
	NT35510_WriteReg(0xD60A, 0x00);
	NT35510_WriteReg(0xD60B, 0x81);

	NT35510_WriteReg(0xD60C, 0x00);
	NT35510_WriteReg(0xD60D, 0xA6);
	NT35510_WriteReg(0xD60E, 0x00);
	NT35510_WriteReg(0xD60F, 0xE5);
	NT35510_WriteReg(0xD610, 0x01);
	NT35510_WriteReg(0xD611, 0x13);
	NT35510_WriteReg(0xD612, 0x01);
	NT35510_WriteReg(0xD613, 0x54);
	NT35510_WriteReg(0xD614, 0x01);
	NT35510_WriteReg(0xD615, 0x82);
	NT35510_WriteReg(0xD616, 0x01);
	NT35510_WriteReg(0xD617, 0xCA);
	NT35510_WriteReg(0xD618, 0x02);
	NT35510_WriteReg(0xD619, 0x00);
	NT35510_WriteReg(0xD61A, 0x02);
	NT35510_WriteReg(0xD61B, 0x01);
	NT35510_WriteReg(0xD61C, 0x02);
	NT35510_WriteReg(0xD61D, 0x34);
	NT35510_WriteReg(0xD61E, 0x02);
	NT35510_WriteReg(0xD61F, 0x67);
	NT35510_WriteReg(0xD620, 0x02);
	NT35510_WriteReg(0xD621, 0x84);
	NT35510_WriteReg(0xD622, 0x02);
	NT35510_WriteReg(0xD623, 0xA4);
	NT35510_WriteReg(0xD624, 0x02);
	NT35510_WriteReg(0xD625, 0xB7);
	NT35510_WriteReg(0xD626, 0x02);
	NT35510_WriteReg(0xD627, 0xCF);
	NT35510_WriteReg(0xD628, 0x02);
	NT35510_WriteReg(0xD629, 0xDE);
	NT35510_WriteReg(0xD62A, 0x02);
	NT35510_WriteReg(0xD62B, 0xF2);
	NT35510_WriteReg(0xD62C, 0x02);
	NT35510_WriteReg(0xD62D, 0xFE);
	NT35510_WriteReg(0xD62E, 0x03);
	NT35510_WriteReg(0xD62F, 0x10);
	NT35510_WriteReg(0xD630, 0x03);
	NT35510_WriteReg(0xD631, 0x33);
	NT35510_WriteReg(0xD632, 0x03);
	NT35510_WriteReg(0xD633, 0x6D);

	//LV2 Page 0 enable
	NT35510_WriteReg(0xF000, 0x55);
	NT35510_WriteReg(0xF001, 0xAA);
	NT35510_WriteReg(0xF002, 0x52);
	NT35510_WriteReg(0xF003, 0x08);
	NT35510_WriteReg(0xF004, 0x00);

	//Display control
	NT35510_WriteReg(0xB100, 0xCC);
	NT35510_WriteReg(0xB101, 0x00);

	//Source hold time
	NT35510_WriteReg(0xB600, 0x05);
	//Gate EQ control
	NT35510_WriteReg(0xB700, 0x70);
	NT35510_WriteReg(0xB701, 0x70);

	//Source EQ control (Mode 2)
	NT35510_WriteReg(0xB800, 0x01);
	NT35510_WriteReg(0xB801, 0x03);
	NT35510_WriteReg(0xB802, 0x03);
	NT35510_WriteReg(0xB803, 0x03);

	//Inversion mode (2-dot)
	NT35510_WriteReg(0xBC00, 0x02);
	NT35510_WriteReg(0xBC01, 0x00);
	NT35510_WriteReg(0xBC02, 0x00);

	//Timing control 4H w/ 4-delay
	NT35510_WriteReg(0xC900, 0xD0);
	NT35510_WriteReg(0xC901, 0x02);
	NT35510_WriteReg(0xC902, 0x50);
	NT35510_WriteReg(0xC903, 0x50);
	NT35510_WriteReg(0xC904, 0x50);

	NT35510_WriteReg(0x3500, 0x00);
	NT35510_WriteReg(0x3A00, 0x55);
	NT35510_WriteReg(0x3600, 0x00);

	NT35510_WR_REG(0x1100);
	Delay_Us(120);
	NT35510_WR_REG(0x2900);

	NT35510_Display_Dir(VDIR);
	NT35510_Clear(WHITE);
	Delay_Ms(200);
	NT35510_LED = 1;
}
//清屏函数
//color:要清屏的填充色
void NT35510_Clear(volatile uint16_t color)
{
	u32 index = 0;
	u32 totalpoint = info.width;
	totalpoint *= info.height; //得到总点数
	// printf("width: %d height: %d and: %d cmd: %x\n",WIDTH,HEIGHT,totalpoint,WRAMCMD);
	NT35510_SetCursor(0x00, 0x00); //设置光标位置
	NT35510_WriteRAM_Prepare();		 //开始写入GRAM
	// printf("Color: %x\r\n",color);
	for (index = 0; index < totalpoint; index++)
	{
		NT35510->NT35510_RAM = color;
	}
}
//在指定区域内填充单个颜色
//(sx,sy),(ex,ey):填充矩形对角坐标,区域大小为:(ex-sx+1)*(ey-sy+1)
//color:要填充的颜色
void NT35510_Fill(uint16_t sx, uint16_t sy, uint16_t ex, uint16_t ey, uint16_t color)
{
	uint16_t i, j;
	uint16_t xlen = 0;
	xlen = ex - sx + 1;
	for (i = sy; i <= ey; i++)
	{
		NT35510_SetCursor(sx, i);		//设置光标位置
		NT35510_WriteRAM_Prepare(); //开始写入GRAM
		for (j = 0; j < xlen; j++)
		{
			NT35510->NT35510_RAM = color; //显示颜色
		}
	}
}
//在指定区域内填充指定颜色块
//(sx,sy),(ex,ey):填充矩形对角坐标,区域大小为:(ex-sx+1)*(ey-sy+1)
//color:要填充的颜色
void NT35510_Color_Fill(uint16_t sx, uint16_t sy, uint16_t ex, uint16_t ey, uint16_t *color)
{
	uint16_t height, width;
	uint16_t i, j;
	width = ex - sx + 1;	//得到填充的宽度
	height = ey - sy + 1; //高度
	for (i = 0; i < height; i++)
	{
		NT35510_SetCursor(sx, sy + i); //设置光标位置
		NT35510_WriteRAM_Prepare();		 //开始写入GRAM
		for (j = 0; j < width; j++)
		{
			NT35510->NT35510_RAM = color[i * width + j]; //写入数据
		}
	}
}
//画线
//x1,y1:起点坐标
//x2,y2:终点坐标
void NT35510_DrawLine(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2)
{
	uint16_t t;
	int xerr = 0, yerr = 0, delta_x, delta_y, distance;
	int incx, incy, uRow, uCol;
	delta_x = x2 - x1; //计算坐标增量
	delta_y = y2 - y1;
	uRow = x1;
	uCol = y1;
	if (delta_x > 0)
	{
		incx = 1; //设置单步方向
	}
	else if (delta_x == 0)
	{
		incx = 0; //垂直线
	}
	else
	{
		incx = -1;
		delta_x = -delta_x;
	}
	if (delta_y > 0)
	{
		incy = 1;
	}
	else if (delta_y == 0)
	{
		incy = 0; //水平线
	}
	else
	{
		incy = -1;
		delta_y = -delta_y;
	}
	if (delta_x > delta_y)
	{
		distance = delta_x; //选取基本增量坐标轴
	}
	else
	{
		distance = delta_y;
	}
	for (t = 0; t <= distance + 1; t++) //画线输出
	{
		NT35510_DrawPoint(uRow, uCol); //画点
		xerr += delta_x;
		yerr += delta_y;
		if (xerr > distance)
		{
			xerr -= distance;
			uRow += incx;
		}
		if (yerr > distance)
		{
			yerr -= distance;
			uCol += incy;
		}
	}
}
//画矩形
//(x1,y1),(x2,y2):矩形的对角坐标
void NT35510_DrawRectangle(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2)
{
	NT35510_DrawLine(x1, y1, x2, y1);
	NT35510_DrawLine(x1, y1, x1, y2);
	NT35510_DrawLine(x1, y2, x2, y2);
	NT35510_DrawLine(x2, y1, x2, y2);
}
//在指定位置画一个指定大小的圆
//(x,y):中心点
//r    :半径
void NT35510_Draw_Circle(uint16_t x0, uint16_t y0, uint8_t r)
{
	int a, b;
	int di;
	a = 0;
	b = r;
	di = 3 - (r << 1); //判断下个点位置的标志
	while (a <= b)
	{
		NT35510_DrawPoint(x0 + a, y0 - b); //5
		NT35510_DrawPoint(x0 + b, y0 - a); //0
		NT35510_DrawPoint(x0 + b, y0 + a); //4
		NT35510_DrawPoint(x0 + a, y0 + b); //6
		NT35510_DrawPoint(x0 - a, y0 + b); //1
		NT35510_DrawPoint(x0 - b, y0 + a);
		NT35510_DrawPoint(x0 - a, y0 - b); //2
		NT35510_DrawPoint(x0 - b, y0 - a); //7
		a++;
		//使用Bresenham算法画圆
		if (di < 0)
		{
			di += 4 * a + 6;
		}
		else
		{
			di += 10 + 4 * (a - b);
			b--;
		}
	}
}
//在指定位置显示一个字符
//x,y:起始坐标
//num:要显示的字符:" "--->"~"
//size:字体大小 12/16/24
//mode:叠加方式(1)还是非叠加方式(0)
void NT35510_ShowChar(uint16_t x, uint16_t y, uint8_t num, uint8_t size, uint8_t mode)
{
	uint8_t temp, t1, t;
	uint16_t y0 = y;
	uint8_t csize = (size / 8 + ((size % 8) ? 1 : 0)) * (size / 2); //得到字体一个字符对应点阵集所占的字节数
	num = num - ' ';																								//得到偏移后的值（ASCII字库是从空格开始取模，所以-' '就是对应字符的字库）
	for (t = 0; t < csize; t++)
	{
		if (size == 12)
		{
			temp = asc2_1206[num][t]; //调用1206字体
		}
		else if (size == 16)
		{
			temp = asc2_1608[num][t]; //调用1608字体
		}
		else if (size == 24)
		{
			temp = asc2_2412[num][t]; //调用2412字体
		}
		else
		{
			return; //没有的字库
		}
		for (t1 = 0; t1 < 8; t1++)
		{
			if (temp & 0x80)
			{
				NT35510_Fast_DrawPoint(x, y, POINT_COLOR);
			}
			else if (mode == 0)
			{
				NT35510_Fast_DrawPoint(x, y, BACK_COLOR);
			}
			temp <<= 1;
			y++;
			if (y >= info.height)
			{
				return; //超区域了
			}
			if ((y - y0) == size)
			{
				y = y0;
				x++;
				if (x >= info.width)
				{
					return; //超区域了
				}
				break;
			}
		}
	}
}
//m^n函数
//返回值:m^n次方.
u32 NT35510_Pow(uint8_t m, uint8_t n)
{
	u32 result = 1;
	while (n--)
	{
		result *= m;
	}
	return result;
}
//显示数字,高位为0,则不显示
//x,y :起点坐标
//len :数字的位数
//size:字体大小
//color:颜色
//num:数值(0~4294967295);
void NT35510_ShowNum(uint16_t x, uint16_t y, u32 num, uint8_t len, uint8_t size)
{
	uint8_t t, temp;
	uint8_t enshow = 0;
	for (t = 0; t < len; t++)
	{
		temp = (num / NT35510_Pow(10, len - t - 1)) % 10;
		if (enshow == 0 && t < (len - 1))
		{
			if (temp == 0)
			{
				NT35510_ShowChar(x + (size / 2) * t, y, ' ', size, 0);
				continue;
			}
			else
			{
				enshow = 1;
			}
		}
		NT35510_ShowChar(x + (size / 2) * t, y, temp + '0', size, 0);
	}
}
//显示数字,高位为0,还是显示
//x,y:起点坐标
//num:数值(0~999999999);
//len:长度(即要显示的位数)
//size:字体大小
//mode:
//[7]:0,不填充;1,填充0.
//[6:1]:保留
//[0]:0,非叠加显示;1,叠加显示.
void NT35510_ShowxNum(uint16_t x, uint16_t y, u32 num, uint8_t len, uint8_t size, uint8_t mode)
{
	uint8_t t, temp;
	uint8_t enshow = 0;
	for (t = 0; t < len; t++)
	{
		temp = (num / NT35510_Pow(10, len - t - 1)) % 10;
		if (enshow == 0 && t < (len - 1))
		{
			if (temp == 0)
			{
				if (mode & 0X80)
				{
					NT35510_ShowChar(x + (size / 2) * t, y, '0', size, mode & 0X01);
				}
				else
				{
					NT35510_ShowChar(x + (size / 2) * t, y, ' ', size, mode & 0X01);
				}
				continue;
			}
			else
			{
				enshow = 1;
			}
		}
		NT35510_ShowChar(x + (size / 2) * t, y, temp + '0', size, mode & 0X01);
	}
}
//显示字符串
//x,y:起点坐标
//width,height:区域大小
//size:字体大小
//*p:字符串起始地址
void NT35510_ShowString(uint16_t x, uint16_t y, uint16_t width, uint16_t height, uint8_t size, uint8_t *p)
{
	uint8_t x0 = x;
	width += x;
	height += y;
	while ((*p <= '~') && (*p >= ' ')) //判断是不是非法字符!
	{
		if (x >= width)
		{
			x = x0;
			y += size;
		}
		if (y >= height)
		{
			break; //退出
		}
		NT35510_ShowChar(x, y, *p, size, 0);
		x += size / 2;
		p++;
	}
}

void NT35510_DMA_Color_Fill(uint16_t sx, uint16_t sy, uint16_t ex, uint16_t ey, uint16_t *color)
{
	uint16_t height, width;
	uint16_t i, j;
	width = ex - sx + 1;	//得到填充的宽度
	height = ey - sy + 1; //高度
	NT35510_Set_Window(sx,sy,width,height);
	
	// for (i = 0; i < height; i++)
	// {
	// 	NT35510_SetCursor(sx, sy + i); //设置光标位置
	// 	NT35510_WriteRAM_Prepare();		 //开始写入GRAM
	// 	for (j = 0; j < width; j++)
	// 	{
	// 		NT35510->NT35510_RAM = color[i * width + j]; //写入数据
	// 	}
	// }
	// DMA_
}